package com.leetroll.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leetroll.api.mapper.FileMapper;
import com.leetroll.api.service.FileService;
import com.leetroll.common.constant.SQLConstant;
import com.leetroll.common.entity.File;
import com.leetroll.common.enums.EFileType;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static com.leetroll.common.utils.AliyunOSSUtil.GetUrl;

@Service
public class FileServiceImpl extends SuperServiceImpl<FileMapper, File> implements FileService {

    @Override
    @Cacheable(cacheNames = "getRescMainFile", key = "#rescId")
    public String getRescMainFile(Long rescId) {
        QueryWrapper<File> fileQueryWrapper = new QueryWrapper<>();
        fileQueryWrapper.eq(SQLConstant.FILE_RESC_ID, rescId);
        fileQueryWrapper.eq(SQLConstant.FILE_TYPE, EFileType.MAIN.getType());
        File file = this.list(fileQueryWrapper).get(0);
        return GetUrl(file.getBucket(), file.getKey() + file.getName() + "." + file.getSuffix());
    }

    @Override
    @Cacheable(cacheNames = "getAttachByRescId", key = "#itemId")
    public File getAttachByRescId(Integer itemId) {
        QueryWrapper<File> fileQueryWrapper = new QueryWrapper<>();
        fileQueryWrapper.eq(SQLConstant.FILE_RESC_ID, itemId);
        fileQueryWrapper.eq(SQLConstant.FILE_TYPE, EFileType.ATTACHMENT.getType());
        List<File> files = this.list(fileQueryWrapper);
        return CollectionUtils.isEmpty(files) ? null : files.get(0);
    }

    @Override
    @Cacheable(cacheNames = "listFileByIdList", key = "#ids")
    public List<File> listByIdList(List<Long> ids) {
        QueryWrapper<File> fileWrapper = new QueryWrapper<>();
        fileWrapper.lambda().in(File::getId, ids);
        return list(fileWrapper);
    }

    @Override
    public List<File> listByRescId(Long rescId) {
        QueryWrapper<File> fileWrapper = new QueryWrapper<>();
        fileWrapper.eq(SQLConstant.FILE_RESC_ID, rescId);
        return list(fileWrapper);
    }

    @Override
    public Boolean saveFile(Long itemId,String key, String name, String suffix, String bucket, String type, Long size,String format) {
        File file = new File();
        //表示该文件属于哪一个item  item的id
        file.setRescId(itemId);
        //文件在阿里云上的存储路径key
        file.setKey(key);
        //文件名名称
        file.setName(name);
        //文件大小(单位: bytes), 直接file.length
        file.setSize(size);
        //文件类型 (不是文件的格式), 指的是该文件属于以下哪一类：主图  预览图  附件  参考枚举类 EFileType
        file.setType(type);
        //文件对应阿里云的bucket名称 枚举类 EBucket.TEST_BUCKET
        file.setBucket(bucket);
        //文件后缀 不要带.   (比如jpg, 不要.jpg)
        file.setSuffix(suffix);
        //3dmax, object, fbx适用平台 (这里逗号分割入库)
        file.setPlatform("3dmax,object,fbx");
        //文件格式 这个字段和suffix一致， 但如果是附件类型，这里为3dmax
        file.setFormat(format);

        return this.save(file);
    }
}
